
package com.shaoshaohuo.app.view;

import com.shaoshaohuo.app.utils.StringHelper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private int xPoint = 0;
    private int yPoint = 0;
    private Context mContext;
    
    private int CurrentPosition;

    public CustomScrollView(Context context) {
        super(context);
        this.mContext = context;
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        setFadingEdgeLength(0);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        setFadingEdgeLength(0);
    }

    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xPoint = (int) ev.getX();
                yPoint = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int newXPoint = (int) ev.getX();
                int newYPoint = (int) ev.getY();

                // System.out.println("xPoint:"+xPoint+"--newXPoint："+newXPoint);
                // System.out.println("yPoint:"+yPoint+"--newYPoint："+newYPoint);

                // if (Math.abs(newYPoint - yPoint) > IUtil.dip2px(mContext, 30)
                // && Math.abs(newXPoint - xPoint) < IUtil.dip2px(mContext, 30))
                // {
                // xPoint = newXPoint;
                // yPoint = newYPoint;
                // return true;
                // }else{
                // if((Math.abs(newYPoint - yPoint)*1.5f) < Math.abs(newXPoint -
                // xPoint)){
                // xPoint = newXPoint;
                // yPoint = newYPoint;
                // return false;
                // }else if(Math.abs(newYPoint - yPoint)>0 && Math.abs(newXPoint
                // - xPoint) < IUtil.dip2px(mContext, 5)){
                // xPoint = newXPoint;
                // yPoint = newYPoint;
                // return true;
                // }
                //
                // return false;
                // }

                if (Math.abs(newYPoint - yPoint) < StringHelper.dip2px(mContext,10) && Math.abs(newXPoint - xPoint) < StringHelper.dip2px(mContext,10)) {
                    xPoint = newXPoint;
                    yPoint = newYPoint;
                    return super.onInterceptTouchEvent(ev);
                }

                if (Math.abs(newYPoint - yPoint) > Math.abs(newXPoint - xPoint)) {
                    xPoint = newXPoint;
                    yPoint = newYPoint;
                    return true;
                }
                
                xPoint = newXPoint;
                yPoint = newYPoint;
                return false;

                // break;
            case MotionEvent.ACTION_UP:
                xPoint = 0;
                yPoint = 0;
                break;
        }
//        return false;
               
        return super.onInterceptTouchEvent(ev) ;
        /*
                                                * && new GestureDetector(new
                                                * YScrollDetector
                                                * ()).onTouchEvent(ev)
                                                ;*/
        // return super.onInterceptTouchEvent(ev);
    }

//    class YScrollDetector extends SimpleOnGestureListener {
//        @Override
//        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//            if (distanceY != 0 && distanceX != 0) {
//
//            }
//            if (Math.abs(distanceY) >= Math.abs(distanceX)) {
//                return true;
//            }
//            return false;
//        }
//    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
